clear all
set more off

do "set_path.do"

global file_crsp_daily	"${datadir}/treasury/crsp_daily_raw_2010_2025.csv"
global file_gdp_latest	"${datadir}other/gdp.dta"

global cipdata 		"${datadir}cip_all_both.dta"
global crspdata		"${datadir}treasury/crsp_long_daily.dta"
global cassidy_iv 	"${datadir}other/cassidy_iv.dta"


***** I. Pre-process data *****

*** I.1. Pre-process US GDP data ***
use ${file_gdp_latest}, clear
keep if currency=="USD"
gen date_q = qofd(date)
format %tq date_q
save "${tempdir}us_gdp_q.dta", replace

/*
*** I.2. Pre-process Treasury outstanding data [Take time to run] ***
import delimited ${file_crsp_daily}, clear
gen is_bill = 1 if inlist(itype, 4, 8)	// 4 = Treasury bill, 8 = Tax anticipation bill
replace is_bill = 0 if missing(is_bill)
gen date = date(caldt,"YMD")
format %td date 

collapse (sum) amt_outst=tdtotout, by(date is_bill)
reshape wide amt_outst, i(date) j(is_bill)
rename (amt_outst0 amt_outst1) (bond_outst bill_outst)
save "${datadir}other/tsy_balance.dta", replace
*/


***** II. DCIV Regression *****

use ${cipdata}, clear

* Fill-in data for missing date
keep group currency tenor date cip_govt
reshape wide cip_govt, i(group tenor date) j(currency) string
reshape long cip_govt, i(group tenor date) j(currency) string
sort group currency tenor date
bys group currency tenor: replace cip_govt = cip_govt[_n-1] if missing(cip_govt) & !missing(cip_govt[_n-1])

* Merge with Treasury balance
mmerge date using "${datadir}other/tsy_balance.dta", type(n:1) unmatch(master) 
bys group currency tenor (date): replace bill_outst = bill_outst[_n-1] if missing(bill_outst) & !missing(bill_outst[_n-1])
bys group currency tenor (date): replace bond_outst = bond_outst[_n-1] if missing(bond_outst) & !missing(bond_outst[_n-1])
gen total_tsy_outst = bill_outst + bond_outst
gen tsy_outst = bill_outst if inlist(tenor,"3m","1y")
replace tsy_outst = bond_outst if !inlist(tenor,"3m","1y")

* Merge with debt/gdp
gen date_q = qofd(date)
format %tq date_q

mmerge date_q using "${tempdir}us_gdp_q.dta", type(n:1) unmatch(master)
sort date_q date

mmerge currency date_q using "${tempdir}debt_q.dta", type(n:1) unmatch(master) umatch(currency quarter)
gen ln_gr_dpgdp = ln(govt_debt/ngdp_a_lc)

bys group currency tenor date_q (date): replace ngdp_a = . if _n<_N
bys group currency tenor: ipolate ngdp_a date, gen(_ngdp_a)

replace ngdp_a = _ngdp_a if missing(ngdp_a)
replace ngdp_a = ngdp_a / 1e6
drop date_q ngdp _*

gen bill_gdp  = bill_outst/ngdp_a
gen bond_gdp  = bond_outst/ngdp_a
gen total_tsy_gdp = total_tsy_outst/ngdp_a
gen tsy_gdp   = tsy_outst/ngdp_a


* Merge with debt ceiling episode
mmerge date using ${cassidy_iv}, type(n:1) unmatch(master)

bys group currency tenor (date): replace episode = episode[_n-1] if missing(episode) & !missing(episode[_n-1]) & ///
												   (!missing(episode[_n+1]) | !missing(episode[_n+2]) | ///
												    !missing(episode[_n+3]) | !missing(episode[_n+4]))
bys group currency tenor (date): replace dciv = dciv[_n-1] if missing(dciv) & !missing(dciv[_n-1]) & ///
												(!missing(dciv[_n+1]) | !missing(dciv[_n+2]) | ///
												 !missing(dciv[_n+3]) | !missing(dciv[_n+4]))
bys group currency tenor (date): replace seiv = seiv[_n-1] if missing(seiv) & !missing(seiv[_n-1]) & ///
												(!missing(seiv[_n+1]) | !missing(seiv[_n+2]) | ///
												 !missing(seiv[_n+3]) | !missing(seiv[_n+4]))
bys group currency tenor (date): replace tgaiv = tgaiv[_n-1] if missing(tgaiv) & !missing(tgaiv[_n-1]) & ///
												(!missing(tgaiv[_n+1]) | !missing(tgaiv[_n+2]) | ///
												 !missing(tgaiv[_n+3]) | !missing(tgaiv[_n+4]))
bys group currency tenor (date): replace iv = iv[_n-1] if missing(iv) & !missing(iv[_n-1]) & ///
												(!missing(iv[_n+1]) | !missing(iv[_n+2]) | ///
												 !missing(iv[_n+3]) | !missing(iv[_n+4]))
gen dciv_dummy = 1 if !missing(dciv)
replace dciv_dummy = 0 if missing(dciv_dummy)

* Merge with VIX
mmerge date using "${datadir}other/vix_usd.dta",type(n:1) unmatch(master) ukeep(VIXCLS) urename(VIXCLS vix)
bys group currency tenor (date): replace vix = vix[_n-1] if missing(vix) & !missing(vix[_n-1])

sort group currency tenor date
gen ln_tsy_gdp = ln(tsy_gdp)

* Label variables
la var cip_govt "$\Phi$"
la var dciv_dummy "Debt ceiling negotiation"
la var vix "VIX"
la var ln_tsy_gdp "$\log (\frac{\text{Debt}}{\text{GDP}})\_{\text{US},\text{n}}$"
la var ln_gr_dpgdp "$\log (\frac{\text{Debt}}{\text{GDP}})\_{\text{i}}$"

* Regressions
egen curr_group = group(currency)
egen tenor_group = group(tenor)
egen all_group = group(group currency tenor)
egen time = group(date)
xtset all_group time

eststo clear
eststo: xtscc cip_govt dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix if tenor=="3m" & group=="g10" & date>td(31dec2010) & date<=td(31dec2024), fe lag(91)
eststo: xtscc cip_govt dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix if tenor=="1y" & group=="g10" & date>td(31dec2010) & date<=td(31dec2024), fe lag(91)
eststo: xtscc cip_govt dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix if tenor=="5y" & group=="g10" & date>td(31dec2010) & date<=td(31dec2024), fe lag(91)
eststo: xtscc cip_govt dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix if tenor=="10y" & group=="g10" & date>td(31dec2010) & date<=td(31dec2024), fe lag(91)

esttab est*, k(dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix) starlevels(* 0.10 ** 0.05 *** 0.01) b(%7.2f) se label indicate( `r(indicate_fe)') stats(N r2_w,  labels("Observations" "\textit{R}$^{2}$")) title("Debt Ceiling Regression") mlabel("\Phi_{\text{3m}}" "\Phi_{\text{1y}}" "\Phi_{\text{5y}}" "\Phi_{\text{10y}}" "\Phi_{\text{3m}}" "\Phi_{\text{1y}}" "\Phi_{\text{5y}}" "\Phi_{\text{10y}}")

esttab using "${outdir}tables/dciv_tenors.tex", k(dciv_dummy ln_tsy_gdp ln_gr_dpgdp vix) b(%9.2f) se(%9.2f) label indicate( `r(indicate_fe)') stats(N r2_w, fmt(%9.0fc %9.2f) labels("Observations" "\textit{R}$^{2}$")) title("Debt Ceiling Regression") star(* 0.10 ** 0.05 *** 0.01) mlabel("\Phi_{\text{3m}}" "\Phi_{\text{1y}}" "\Phi_{\text{5y}}" "\Phi_{\text{10y}}" "\Phi_{\text{3m}}" "\Phi_{\text{1y}}" "\Phi_{\text{5y}}" "\Phi_{\text{10y}}") substitute(\_ _) prehead("\begin{tabular}{l*{@M}{c}} \hline \hline") prefoot("\hline") postfoot("\hline \hline \end{tabular}") replace 
